<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>王者荣耀轮播无限轮播</title>
  <link rel="stylesheet" href="./css/reset.css">
  <link rel="stylesheet" href="./css/common.css">
  <style>
    .main {
      height: 100px;
    }

    .news-section {
      display: flex;
      height: 342px;
    }

    .news-section .banner {
      position: relative;
      width: 605px;
      background-color: #000;
      overflow: hidden;
    }

    .news-section .banner:hover .control {
      display: block;
    }

    .news-section .banner .image-list {
      display: flex;
      width: 604px;
      transform: translateX(-100%);
    }

    .news-section .banner .image-list .item {
      flex-shrink: 0;
      width: 100%;
    }

    .news-section .banner .image-list .item a {
      display: block;
    }

    .news-section .banner .image-list .item a img {
      width: 100%;
    }

    .news-section .banner .control {
      position: absolute;
      top: 50%;
      transform: translateY(-50%);
      width: 36px;
      height: 36px;
      display: none;
      border-radius: 50%;
      background-color: rgba(0, 0, 0, .4);
    }

    .news-section .banner .control.left {
      left: 10px;
    }

    .news-section .banner .control.right {
      right: 10px;
    }

    .news-section .banner .title-list {
      display: flex;
      height: 44px;
      line-height: 44px;
    }

    .news-section .banner .title-list .item {
      flex: 1;
      text-align: center;
    }

    .news-section .banner .title-list .item a {
      display: block;
      font-size: 14px;
      color: #b1b2be;
    }

    .news-section .banner .title-list .item.active a,
    .news-section .banner .title-list .item a:hover {
      color: #f3c258;
      background-color: rgba(255, 255, 255, .15);
    }

    .news-section .news {
      flex: 1;
      background-color: purple;
    }

    .news-section .download {
      width: 236px;
      background-color: skyblue;
    }

    .news-section .download a {
      display: block;
      background: url(./img/main_sprite.png) no-repeat;
    }

    .news-section .download a.download-btn {
      height: 128px;
      background-position: 0 -219px;
    }

    .news-section .download a.guard-btn {
      height: 106px;
      background-position: 0 -350px;
    }

    .news-section .download a.experience-btn {
      height: 108px;
      background-position: 0 -461px;
    }
  </style>
</head>

<body>

  <div class="main main_wrapper">
    <div class="news-section">
      <div class="banner">
        <ul class="image-list"></ul>
        <ul class="title-list"></ul>
      </div>
      <div class="news"></div>
      <div class="download">
        <a class="download-btn" href="#"></a>
        <a class="guard-btn" href="#"></a>
        <a class="experience-btn" href="#"></a>
      </div>
    </div>
  </div>

  <script>
    const bannerData = [
      {
        title: "桑启的旅途故事",
        img: "./img/banner_01.jpeg",
        active: true
      },
      {
        title: "启示之音抢先听",
        img: "./img/banner_02.jpeg",
      },
      {
        title: "谁成为版本之子",
        img: "./img/banner_03.jpeg",
      },
      {
        title: "观赛体验升级",
        img: "./img/banner_04.jpeg",
      },
      {
        title: "季后赛开战",
        img: "./img/banner_05.jpeg",
      },
    ]

    let currentIndex = 0
    const timeout = 500
    const bannerEl = document.querySelector(".banner")
    const imageListEl = document.querySelector(".image-list")
    imageListEl.style.transition = `transform ${timeout}ms ease`
    const titleListEl = document.querySelector(".title-list")

    bannerData.forEach((item, index) => {
      const isActive = item.active ? "active" : ""
      const imageItemEl = `
        <li class="item">
          <a href="#">
            <img src="${item.img}" alt="">
          </a>
        </li>
      `
      const titleItemEl = `
        <li class="item ${isActive}" data-index="${index}">
          <a href="#">${item.title}</a>
        </li>
      `
      imageListEl.innerHTML += imageItemEl
      titleListEl.innerHTML += titleItemEl
    })

    function cloneElhandler() {
      const firstImageItemEl = imageListEl.firstElementChild.cloneNode(true)
      const lastImageItemEl = imageListEl.lastElementChild.cloneNode(true)
      imageListEl.append(firstImageItemEl)
      imageListEl.prepend(lastImageItemEl)
    }
    cloneElhandler()

    function changeControlEl(className, method) {
      const controlEl = document.createElement("div")
      controlEl.classList.add("control", className)
      controlEl.addEventListener("click",  () => {
        if (method === "add") {
          currentIndex++
        } else if (method === "sub"){
          currentIndex--
        }
        switchBannerHandler()
      })
      bannerEl.append(controlEl)
    }
    changeControlEl('right', 'add')
    changeControlEl('left', 'sub')

    let titleActiveEl = document.querySelector(".title-list .active")
    titleListEl.onmouseover = function (e) {
      const target = e.target.parentElement
      if (target.classList.contains("item")) {
        currentIndex = target.dataset.index
        clearAutoBannerHandler()
        switchBannerHandler()
      }
    }
    titleListEl.onmouseout = function (e) {
      autoBannerHandler()
    }

    function switchBannerHandler() {
      let index = (parseInt(currentIndex) + 1) * 100
      imageListEl.style.transform = `translateX(${-index}%)`
      if (index === (parseInt(bannerData.length) + 1) * 100) {
        currentIndex = 0
        setTimeout(() => {
          imageListEl.style.transition = "none"
          imageListEl.style.transform = `translateX(-${100}%)`
        }, timeout);
      } else if (index === 0) {
        currentIndex = parseInt(bannerData.length) - 1
        setTimeout(() => {
          imageListEl.style.transition = "none"
          imageListEl.style.transform = `translateX(-${bannerData.length * 100}%)`
        }, timeout);
      }

      const titleItemEl = titleListEl.children[currentIndex]
      titleActiveEl.classList.remove("active")
      titleItemEl.classList.add("active")
      titleActiveEl = titleItemEl
    }

    let timer = null
    function autoBannerHandler() {
      timer = setInterval(() => {
        currentIndex++
        switchBannerHandler()
      }, 2000)
    }
    autoBannerHandler() // 自动轮播

    function clearAutoBannerHandler() {
      clearInterval(timer)
    }

    // 节流函数
    function throttle(fn, delay) {
      let timer = null
      return function() {
        if (timer) return
        timer = setTimeout(() => {
          fn()
          timer = null
        }, delay)
      }
    }

    // 防抖函数
    function debounce(fn, delay) {
      let timer = null
      return function() {
        if (timer) {
          clearTimeout(timer)
        }
        timer = setTimeout(() => {
          fn()
        }, delay)
      }
    }

  </script>

</body>

</html>